<template>
	<view>
		<scroll-view :scroll-x="true">
			<view class="product-list">
				<view class="product-item " v-for="(item, index) in compareList" :key="item.id">
					<image :src="item.productImg.split(',')[0] | formatFile" class="cover" mode=""></image>
					<view class="bottom-info">
						<view class="name">{{item.productTitle}}</view>
						<view class="price">{{item.showPrice | formatMoney}}</view>
					</view>
				</view>
			</view>
		</scroll-view>
		
		<view class="line"></view>
		
		<view style="padding: 20rpx;background-color: #fff;">
			<view class="product-param">
				<view class="param-item" v-for="(item, index) in productAttr" :key="index">
					<view :class="{'param-name': i == 0, 'param-content': i != 0}" v-for="(entry, i) in item" :key="i">{{entry}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { getDiffResult } from '@/api/product.js'
	export default {
		data() {
			return {
				ids: '',
				compareList: [],
				productAttr:[]
			};
		},
		onLoad(options){
			this.ids = decodeURIComponent(options.ids)
			this.getDiffResult()
		},
		methods:{
			getDiffResult(){
				getDiffResult({productIds: this.ids}).then(res => {
					this.compareList = res.result
					// 获取所有属性名
					const set = new Set();
					res.result.forEach(item => {
						const attrs = JSON.parse(item.productAttr);
						attrs.forEach(item => {
							set.add(item.title)
						})
					})
					const titles = [...set]
					
					
					// 获取所有属性值
					const values = []
					res.result.forEach(item => {
						let arr = []
						const attrs = JSON.parse(item.productAttr);
						for (let title of set){
							const value = attrs.find(attr => attr.title === title)
							if(value){
								arr.push(value.value)
							}else{
								arr.push('-')
							}
						}
						values.push(arr)
					})
					
					// 整合数据
					titles.forEach((title, index) => {
						let arr = []
						arr.push(title)
						
						values.forEach(value => {
							arr.push(value[index])
						})
						this.productAttr.push(arr)
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	
	.product-list {
		display: flex;
		white-space: nowrap;
		padding: 20rpx 30rpx;

		.product-item {
			display: flex;
			flex-direction: column;
			width: 260rpx;
			margin-right: 30rpx;

			.cover {
				width: 260rpx;
				height: 180rpx;
				border-radius: 10rpx;
			}

			.bottom-info {
				display: flex;
				flex-direction: column;
				width: 260rpx;
				font-weight: 500;
				color: #222222;
				font-size: 27rpx;
				padding: 10rpx 0;

				.name {
					word-wrap: break-word;
					white-space: normal;
				}

				.price {
					padding-top: 20rpx;
					font-size: 27rpx;
					color: #FF3634;
				}
			}
		}
	}
	
	.line{
		width: 750rpx;
		height: 19rpx;
		background: #F7F7F7;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
	}
	
	.product-param {
		display: table;
		width: 100%;
		box-sizing: border-box;
		background-color: #fff;
		border-collapse: collapse;
	
		.param-item {
			display: table-row;
			font-size: 29rpx;
			
			
			.param-name, 
			.param-content{
				border: 1rpx solid #E6E6E6;
				display: table-cell;
				width: 25%;
				vertical-align: middle;
				text-align: center;
				padding: 20rpx 10rpx;
			}
			
			.param-name{
				background-color: #fff;
				font-weight: bold;
			}
			
			.param-content:nth-of-type(even){
				background: #FAFAFA;
			}
		}
	}
</style>